package com.example.gulimall.ssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 平淡还心动
 * @createTime: 2020-06-29 19:44
 **/

@Controller
public class HelloController {


    /**
     * 无需登录就可访问
     *
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/hello")
    public String hello() {
        return "hello";
    }

    /**
     * 感知这次是在sso登录成功跳转回来的
     */
    @Value("${sso.server.url}")
    private String ssoServerUrl;
    @GetMapping("/employee")
    public String employees(Model model,HttpSession session,@RequestParam(value = "token",required = false) String token){

        // 如果参数带有token认为是已经在登录服务器登录成功，或者session有值说明也有登录状态
        Object loginUser = session.getAttribute("loginUser");
        // token不为空说明已经在登录服务器登录成功
        if(!StringUtils.isEmpty(token)){
            //TODO 去session获取当前登录token真正的用户信息
            RestTemplate restTemplate=new RestTemplate();
            ResponseEntity<String> forEntity = restTemplate.getForEntity("http://sso.com:8080/userinfo?token=" + token, String.class);
            String body = forEntity.getBody();
            session.setAttribute("loginUser",body);
        }
        if (loginUser==null){
            // 没登陆。跳转到登录服务器登录,跳转过去后使用redirect_url查询参数，登录成功后又跳转回来
            return  "redirect:"+ssoServerUrl+"?redirect_url="+"http://client1.com:8081/employee";
        }else {
            List<String> employees = new ArrayList<>();
            employees.add("张三");
            employees.add("李四");
            model.addAttribute("emps",employees);
            return "employees";
        }

    }

//    @GetMapping(value = "/employees")
//    public String employees(Model model, HttpSession session, @RequestParam(value = "token", required = false) String token) {
//
//        if (!StringUtils.isEmpty(token)) {
//            RestTemplate restTemplate=new RestTemplate();
//            ResponseEntity<String> forEntity = restTemplate.getForEntity("http://sso.mroldx.cn:8080/userinfo?token=" + token, String.class);
//            String body = forEntity.getBody();
//
//            session.setAttribute("loginUser", body);
//        }
//        Object loginUser = session.getAttribute("loginUser");
//
//        if (loginUser == null) {
//
//            return "redirect:" + "http://sso.mroldx.cn:8080/login.html"+"?redirect_url=http://localhost:8081/employees";
//        } else {
//
//
//            List<String> emps = new ArrayList<>();
//
//            emps.add("张三");
//            emps.add("李四");
//
//            model.addAttribute("emps", emps);
//            return "employees";
//        }
//    }

}
